package org.hgvc.webapp.ets.spring.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hgvc.webapp.ets.bo.Package;
import org.hgvc.webapp.ets.bo.PackagePeakRates;
import org.hgvc.webapp.ets.bo.PackageProducts;
import org.hgvc.webapp.ets.bo.PartnerProduct;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.dao.PackageHbmDao;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;


public class AdminPackageServiceImpl implements AdminPackageService{
	private PackageHbmDao dao;
	private String normalPeakType;
	private String superPeakType;

	public String getNormalPeakType() {
		return normalPeakType;
	}
	public String getSuperPeakType() {
		return superPeakType;
	}
	public void setNormalPeakType(String normalPeakType) {
		this.normalPeakType = normalPeakType;
	}
	public void setSuperPeakType(String superPeakType) {
		this.superPeakType = superPeakType;
	}

	public void setDao(PackageHbmDao dao) {
		this.dao = dao;
	}
	
	public List<Package> getPackageList()throws BaseException{
		List<Package> list=new ArrayList<Package>();
		try{
			list=dao.getPackageList();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{Package.class.getSimpleName()}));
		}
		return list;
	}
	
	public List<Package> getPackageList(String searchField,String searchKeyword)throws BaseException{
		List<Package> list=new ArrayList<Package>();
		try{
			list=dao.getPackageList(searchField,searchKeyword);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{Package.class.getSimpleName()}));
		}
		return list;
	}
	
	public List<PackageProducts> getPackageProductsList(Package packageP)throws BaseException{
		List<PackageProducts> list=new ArrayList<PackageProducts>();
		try{
			list=dao.getPackageProductsList(packageP);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{PackageProducts.class.getSimpleName()}));
		}
		return list;
	}
	
	public List<PackagePeakRates> getPackagePeakRatesList(Package packageP,String peakType)throws BaseException{
		List<PackagePeakRates> list=new ArrayList<PackagePeakRates>();
		try{
			
			list=dao.getPackagePeakRatesList(packageP,peakType);
			
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.object", new Object[]{PackagePeakRates.class.getSimpleName()}));
		}
		return list;
	}
	
	public Package savePackage(Package packageP)throws BaseException{
		try{
			dao.save(packageP);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.save.object", new Object[]{Package.class.getSimpleName()}));
		}
		return packageP;
	}
	
	public void updatePackage(Package packageP)throws BaseException{
		try{
			dao.update(packageP);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.update.object", new Object[]{Package.class.getSimpleName()}));
		}
	}
	
	public void savePackageProducts(PackageProducts packageProducts)throws BaseException{
		try{
			Package packageP = (Package)dao.getObject(Package.class, packageProducts.getPackages().getPackageId());
			packageProducts.setPackages(packageP);
			PartnerProduct partnerProduct =(PartnerProduct)dao.getObject(PartnerProduct.class, packageProducts.getPartnerProduct().getPartnerProductId());
			packageProducts.setPartnerProduct(partnerProduct);
			dao.save(packageProducts);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.save.object", new Object[]{PackageProducts.class.getSimpleName()}));
		}
	}
	
	public void updatePackageProducts(PackageProducts packageProducts)throws BaseException{
		try{
			Package packageP = (Package)dao.getObject(Package.class, packageProducts.getPackages().getPackageId());
			packageProducts.setPackages(packageP);
			dao.update(packageProducts);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.update.object", new Object[]{PackageProducts.class.getSimpleName()}));
		}
	}
	
	public void savePackagePeakRates(PackagePeakRates packagePeakRates)throws BaseException{
		try{
			Package packageP = (Package)dao.getObject(Package.class, packagePeakRates.getPackages().getPackageId());
			packagePeakRates.setPackages(packageP);
			dao.save(packagePeakRates);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.save.object", new Object[]{PackagePeakRates.class.getSimpleName()}));
		}
	}
	
	public void updatePackagePeakRates(PackagePeakRates packagePeakRates)throws BaseException{
		try{
			Package packageP = (Package)dao.getObject(Package.class, packagePeakRates.getPackages().getPackageId());
			packagePeakRates.setPackages(packageP);
			dao.update(packagePeakRates);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.update.object", new Object[]{PackagePeakRates.class.getSimpleName()}));
		}
	}
	
	public void deletePackage(List<Package> deleteList)throws BaseException{
		try{
			for(Package packageP : deleteList){
				List<PackageProducts> packageProductsList=dao.getPackageProductsList(packageP);
				if (packageProductsList.size()>0)
					throw new BaseException(SpringUtils.getMessage("error.packageproducts.inused", new Object[]{}));
				
				List<PackagePeakRates> packagePeakRatesList=dao.getPackagePeakRatesList(packageP);
				if (packagePeakRatesList.size()>0)
					throw new BaseException(SpringUtils.getMessage("error.packagepeakrates.inused", new Object[]{}));
				
				packageP.setIsDeleted(CommonConstant.IS_DELETED);
				packageP.setModifiedDate(new Date());
				dao.update(packageP);
			}
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.object", new Object[]{Package.class.getSimpleName()}));
		}
	}
	
	public void deletePackageProducts(List<PackageProducts> deleteList)throws BaseException{
		try{
			for(PackageProducts packageProducts : deleteList){
				packageProducts.setIsDeleted(CommonConstant.IS_DELETED);
				packageProducts.setModifiedDate(new Date());
				dao.update(packageProducts);
			}
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.object", new Object[]{PackageProducts.class.getSimpleName()}));
		}
	}
	
	public void deletePackagePeakRates(List<PackagePeakRates> deleteList)throws BaseException{
		try{
			for(PackagePeakRates packagePeakRates : deleteList){
				packagePeakRates.setIsDeleted(CommonConstant.IS_DELETED);
				packagePeakRates.setModifiedDate(new Date());
				dao.update(packagePeakRates);
			}
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.object", new Object[]{PackagePeakRates.class.getSimpleName()}));
		}
	}
}
